package cn.wikiflyer.beikang.act.user.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.user.UserAddressAAndUAct;
import cn.wikiflyer.beikang.act.user.bean.BKAddressResultBean;
import cn.wikiflyer.beikang.utils.AddressUtil;
import cn.wk.libs4a.WKApplication;

/**
 * 地址数据适配器
 */
public class AddressAdapter extends BaseAdapter {
	private Context context;
	private ArrayList<BKAddressResultBean> listData;
	private OnCheckListener onCheckListener;

	/**
	 * 设置item的监听
	 */
	public void setOnCheckListener(OnCheckListener onCheckListener) {
		this.onCheckListener = onCheckListener;
	}

	public AddressAdapter(Context context) {
		super();
		this.context = context;
		listData = new ArrayList<BKAddressResultBean>();
	}

	public void setData(ArrayList<BKAddressResultBean> listData) {
		this.listData = listData;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if (listData != null) {
			return listData.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if (listData != null) {
			return listData.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public boolean isEnabled(int position) {
		return false;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder mViewHolder = null;
		if (convertView == null) {
			mViewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.act_user_address_list_item, null);
			mViewHolder.linear = (LinearLayout) convertView
					.findViewById(R.id.add_item_linear);
			mViewHolder.tv_name = (TextView) convertView
					.findViewById(R.id.addlist_item_username);
			mViewHolder.tv_phone = (TextView) convertView
					.findViewById(R.id.addlist_item_userphone);
			mViewHolder.tv_address = (TextView) convertView
					.findViewById(R.id.addlist_item_useraddress);
			mViewHolder.btn_del = (Button) convertView
					.findViewById(R.id.addlist_item_delbtn);
			mViewHolder.btn_edit = (Button) convertView
					.findViewById(R.id.addlist_item_editbtn);
			mViewHolder.check_box = (CheckBox) convertView
					.findViewById(R.id.addlist_item_check);
			convertView.setTag(mViewHolder);
		} else {
			mViewHolder = (ViewHolder) convertView.getTag();
		}
		mViewHolder.tv_name.setText("" + listData.get(position).receiver);
		mViewHolder.tv_phone.setText("" + listData.get(position).phone);
		String matchAddress = new AddressUtil(context).matchAddress(
				listData.get(position).sheng, listData.get(position).shi,
				listData.get(position).qu);
		mViewHolder.tv_address.setText(matchAddress
				+ listData.get(position).address);

		mViewHolder.linear.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// Intent mIntent = new Intent();
				// mIntent.setClass(context, UserAddressInfoAct.class);
				// mIntent.putExtra("addressbean", listData.get(position));
				// context.startActivity(mIntent);
			}
		});

		final int default_add = Integer
				.valueOf(listData.get(position).default_add);
		if (default_add == 1) {
			mViewHolder.check_box.setChecked(true);
		} else {
			mViewHolder.check_box.setChecked(false);
		}
		mViewHolder.check_box.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (default_add != 1) {
					onCheckListener.checkBox(listData.get(position).id);
				} else {
					((CheckBox) v).setChecked(true);
				}
			}
		});
		mViewHolder.btn_edit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent mIntent = new Intent();
				// mIntent.setClass(context, UserAddressInfoAct.class);
				mIntent.setClass(context, UserAddressAAndUAct.class);
				mIntent.putExtra("addressbean", listData.get(position));
				context.startActivity(mIntent);
			}
		});
		mViewHolder.btn_del.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				onCheckListener.delete(listData.get(position).id);
			}
		});
		return convertView;
	}

	class ViewHolder {
		LinearLayout linear;
		TextView tv_name;
		TextView tv_phone;
		TextView tv_address;
		CheckBox check_box;
		Button btn_edit; // 编辑地址
		Button btn_del;
	}

	/**
	 * Item操作回调接口
	 */
	public interface OnCheckListener {
		public void checkBox(String addid);

		public void delete(String addid);
	}

}
